<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<style>
    .layui-layer-btn .layui-layer-btn1 {
        border-color: #FF5722;
        background-color: #FF5722;
        color: #fff;
    }
    .layui-table-cell {
        height: inherit;
    }
</style>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>优惠券</legend>
        <div class="layui-field-box">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <select name="status">
                                    <option value="">全部</option>
                                    <option value="0">已领完</option>
                                    <option value="1">已失效</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body">
                    <div style="padding-bottom: 10px;">
                        <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
                    </div>
                    <table id="demo" lay-filter="demo"></table>
                    <script type="text/html" id="discount">
                        {{# if(d.form == '0'){ }}
                        {{d.discount}}元
                        {{# }else{ }}
                        订单总价的{{d.discount}}折
                        {{# } }}
                    </script>
                    <script type="text/html" id="limit">
                        {{# if(d.limit == '0'){ }}
                        不限
                        {{# }else{ }}
                        限领{{d.limit}}份
                        {{# } }}
                    </script>
                    <script type="text/html" id="valid">
                        {{# if(d.term == '0'){ }}
                        {{d.valid}}
                        {{# }else{ }}
                        领券后{{d.valid}}天内
                        {{# } }}
                    </script>
                    <script type="text/html" id="table-content-list">
                        {{# if (d.status == 1){ }}
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="shi"><i class="layui-icon layui-icon-delete"></i>使失效</a>
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="song">发送</a>
                        {{# }else{ }}
                        <a class="layui-btn layui-btn-primary layui-btn-xs">已失效</a>
                        {{# } }}
                    </script>
                </div>
            </div>
        </div>
    </fieldset>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'laydate', 'table'], function(){
        var $ = layui.$,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate;

        laydate.render({
            elem: '#test6'
            ,range: true
            ,type: 'datetime'
        });

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('demo', {
                url: "{:url('Product/coupon')}",
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        //执行渲染
        table.render({
            elem: '#demo',
            url: "{:url('Product/coupon')}",
            method: 'post',
            cols: [[
                {field: 'name', title: '优惠券名称', width: 400},
                {field: 'discount', title: '价值', templet: "#discount"},
                {field: 'limit', title: '领取限制', templet: "#limit"},
                {field: 'valid', title: '有效期', width: 320, templet: "#valid"},
                {field: 'receive', title: '已领取'},
                {field: 'use', title: '已使用'},
                {fixed: 'right', title: '操作', width: 200, align:'center', toolbar: '#table-content-list'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        //监听事件
        table.on('tool(demo)', function(obj){
            var id = obj.data.id;
            var layEvent = obj.event;

            if(layEvent === 'edit'){ //编辑
                layer.open({
                    type: 2
                    ,title: '编辑优惠券'
                    ,content: '/admin/Product/coupon_operate/id/' + id
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        submit.click();
                    }
                });
            }else if(layEvent === 'shi'){
                layer.confirm('确定使优惠券失效吗?', {
                    icon: 3,
                    title:'提示',
                    offset: ['25%', '35%']
                }, function(){
                    $.post("{:url('Product/coupon_shi')}", {id:id}, function(e){
                        if (e == 1){
                            layer.msg('操作成功', {offset: ['30%', '40%']});
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else{
                            layer.msg('操作失败', {offset: ['30%', '40%']});
                        }
                    }, 'json');
                });
            }else if(layEvent === 'song'){
                layer.open({
                    type: 2
                    ,title: '发送优惠券'
                    ,content: '/admin/Product/coupon_song/id/' + id
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        submit.click();
                    }
                });
            }
        });

        //事件
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加优惠券'
                    ,content: "{:url('Product/coupon_operate')}"
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
